package com.dhsc.service.impl.wskh;

import com.dhsc.domain.ywzx.wskh.WskhShjdVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.wskh.IWskhShjdDao;
import com.dhsc.service.wskh.IWskhShjdService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName WskhShjdServiceImpl
 * @description: 网上开户权限配置
 * @author: gx
 * @create: 2024-10-09 16:52:06
 **/
@Service
@Slf4j
public class WskhShjdServiceImpl implements IWskhShjdService {

    @Autowired
    private IWskhShjdDao wskhShjdDao;

    @Override
    public LayuiTableVo queryWskhShjd(WskhShjdVo wskhShjdVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(wskhShjdVo.getPage(),wskhShjdVo.getLimit());
            List<WskhShjdVo> list = wskhShjdDao.queryWskhShjd(wskhShjdVo);
            PageInfo<WskhShjdVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询网上开户权限配置出错", e);
            throw new MyException("查询网上开户权限配置出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveWskhShjd(WskhShjdVo wskhShjdVo) {
        ResponseObject obj = new ResponseObject();
        try {
            if (!"1".equals(wskhShjdVo.getStatus())){
                wskhShjdDao.updateWskhShjd(wskhShjdVo);
            }else{
                WskhShjdVo shjdVo = new WskhShjdVo();
                shjdVo.setUserid(wskhShjdVo.getUserid());
                List<WskhShjdVo> wskhShjdVos = wskhShjdDao.queryWskhShjd(shjdVo);
                if (CommonUtil.isNotEmptyList(wskhShjdVos)){
                    obj.setStatus(202);
                    obj.setMessage("该用户已存在");
                    return obj;
                }
                wskhShjdDao.insertWskhShjd(wskhShjdVo);
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("保存网上开户权限配置出错",e);
            throw new MyException("保存网上开户权限配置出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject deleteWskhShjd(WskhShjdVo wskhShjdVo) {
        ResponseObject obj = new ResponseObject();
        try {
            wskhShjdDao.deleteWskhShjd(wskhShjdVo);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("删除网上开户权限配置出错",e);
            throw new MyException("删除网上开户权限配置出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryWskhShjdYhxx() {
        ResponseObject obj = new ResponseObject();
        try {
            List<WskhShjdVo> list = wskhShjdDao.queryWskhShjdYhxx();
            obj.setData(list);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("删除网上开户权限配置出错",e);
            throw new MyException("删除网上开户权限配置出错",e);
        }
        return obj;
    }
}
